package Infinite::Sequence::SetOperation;
use base qw{Infinite::Sequence};

sub new {
    my( $class, $A, $B, $op ) = @_;
    bless { A => $A, B => $B, op => $op }, $class
}

sub increment {
    my( $this ) = @_;
    while( 1 ) {
        $this->prepare( $this->{A}, $this->{B} );
        
        my( $a, $b ) = ( $this->{a}, $this->{b} );
        
        if( $a < $b ) {
            $this->{a} = undef;
            return $a unless $this->{op} eq q{intersection}
        } elsif( $b < $a ) {
            $this->{b} = undef;
            return $b unless grep /^$this->{op}$/, qw{intersection complement}
        } else {
            $this->{a} = $this->{b} = undef;
            return $a unless $this->{op} eq q{complement}
        }
    }
}

sub prepare {
	my( $this ) = @_;
	$this->{a} = $this->{A}->increment unless defined $this->{a};
	$this->{b} = $this->{B}->increment unless defined $this->{b};
}

1
